<?php

/**
 * Login page
 *
 * @since ADD-ADOdb-Admin 0.0
 */
CLASS ctrl_page_login EXTENDS ctrl_tpl_adodb_admin {

   protected $mode_gpc_login = array(
         '_POST' => array('type','host','username','password','database')
      );


   protected $mode_gpc_logout = array();

   # @see
   protected static $adodb_database_types = array(
         'vfp'          => 'Microsoft Visual FoxPro',
         'mssql'        => 'Microsoft SQL Server 7 and later',
         'mssqlpo'      => 'Portable mssql driver',
         'mysql'        => 'MySQL without transaction support',
         'mysqlt'       => 'MySQL with transaction support',
         'maxsql'       => 'MySQL with transaction support',
         'oci8'         => 'Oracle 8/9',
         'oci8po'       => 'Oracle 8/9 portable driver',
         'odbc'         => 'Generic ODBC',
         'odbc_mssql'   => 'MSSQL ODBC',
         'postgres'     => 'Generic PostgreSQL driver',
         'postgres64'   => 'PostgreSQL 6.4 and earlier',
         'postgres7'    => 'PostgreSQL 7',
         'postgres8'    => 'Postgresql 8',
         'postgres9'    => 'Postgresql 9',

      );

   /**
    * Common Pre Mode Process
    *
    * @since ADD ADOdb Admin 0.1
    */
   public function pre_mode_process( $common_gpc = array() ) {
      $adodb_database_types = static::$adodb_database_types;
      foreach ($adodb_database_types as $adodb_database_key => &$adodb_database_type) {
         $adodb_database_type = "$adodb_database_key: $adodb_database_type";
      }

      $this->assign('adodb_database_types',$adodb_database_types);
   }

   /**
    * Login mode
    *
    * @param array $gpc
    *
    * @since ADD-ADOdb-Admin 0.0
    */
   public function process_mode_login($gpc) {
      extract($gpc);

      if (adodb_user::login($type,$host,$username,$password,$database)) {

         setcookie('login_args[type]',$gpc['type'], time()+(86400*90));
         setcookie('login_args[host]',$gpc['host'], time()+(86400*7));
         setcookie('login_args[username]',$gpc['username'], time()+(86400*7));
         setcookie('login_args[database]',$gpc['database'], time()+(86400*7));


         add::redirect(add::config()->path);
      }

   }


   /**
    * Login mode
    *
    * @param array $gpc
    *
    * @since ADD-ADOdb-Admin 0.0
    */
   public function process_mode_logout($gpc) {
      adodb_user::logout();
   }


   /**
    * Post Mode process
    *
    * @since ADD ADOdb Admin 0.1
    */
   public function post_mode_process( $common_gpc ) {
      foreach ($_COOKIE['login_args'] as $index => $argument) {
         if (empty($this->data[$index]))
            $this->assign($index, $argument);
      }

   }

}